package com.ludaxu.google.view;

import com.ludaxu.google.R;
import com.ludaxu.google.manager.ThreadManager;
import com.ludaxu.google.tools.UiUtils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;

/**
 * 创建了自定义的帧布局 把baseFragment一部分代码 抽取到这个类中,简化baseFragment
 * 
 * @author ludaxu
 * 
 */
public abstract class LoadingPage extends FrameLayout {
	public static final int STATE_UNKOWN = 0;
	public static final int STATE_LOADING = 1;
	public static final int STATE_ERROR = 2;
	public static final int STATE_EMPTY = 3;
	public static final int STATE_SUCCESS = 4;
	public int state = STATE_UNKOWN;// 不能设置成静态的,静态的话,左右子类都复用这个状态了

	private View loadingView;// 加载中的界面
	private View errorView;// 错误界面
	private View emptyView;// 空界面
	private View successView;// 加载成功的界面

	public LoadingPage(Context context) {
		super(context);
		init();
	}

	@SuppressLint("NewApi")
	public LoadingPage(Context context, AttributeSet attrs, int defStyleAttr,
			int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
		init();
	}

	public LoadingPage(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	public LoadingPage(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init() {

		loadingView = createLoadingView(); // 创建了加载中的界面
		if (loadingView != null) {
			this.addView(loadingView, new FrameLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		}
		errorView = createErrorView(); // 加载错误界面
		if (errorView != null) {
			this.addView(errorView, new FrameLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		}
		emptyView = createEmptyView(); // 加载空的界面
		if (emptyView != null) {
			this.addView(emptyView, new FrameLayout.LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		}
		showPage();// 根据不同的状态显示不同的界面

	}

	// 根据不同状态显示不同的界面
	private void showPage() {
		if (loadingView != null) {
			loadingView.setVisibility(state == STATE_UNKOWN
					|| state == STATE_LOADING ? View.VISIBLE : View.INVISIBLE);
		}
		if (errorView != null) {
			errorView.setVisibility(state == STATE_ERROR ? View.VISIBLE
					: View.INVISIBLE);

		}
		if (emptyView != null) {
			emptyView.setVisibility(state == STATE_EMPTY ? View.VISIBLE
					: View.INVISIBLE);
		}
		if (state == STATE_SUCCESS) {

			if (successView == null) {
				successView = createSuccessView();
				this.addView(successView,
						FrameLayout.LayoutParams.MATCH_PARENT,
						FrameLayout.LayoutParams.MATCH_PARENT);

			}
			successView.setVisibility(View.VISIBLE);
		} else {
			if (successView != null) {
				successView.setVisibility(View.INVISIBLE);
			}
		}

	}

	/*
	 * 创建一个空的界面
	 * 
	 * @return
	 */
	private View createEmptyView() {
		View view = View.inflate(UiUtils.getContext(), R.layout.loadpage_empty,
				null);
		return view;
	}

	/*
	 * 创建了一个错误的界面
	 * 
	 * @return
	 */
	private View createErrorView() {
		View view = View.inflate(UiUtils.getContext(), R.layout.loadpage_error,
				null);
		Button pager_bt = (Button) view.findViewById(R.id.page_bt);
		pager_bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				show();
			}
		});
		return view;
	}

	/*
	 * 创建加载界面
	 */
	private View createLoadingView() {
		View view = View.inflate(UiUtils.getContext(),
				R.layout.loadpage_loading, null);
		return view;
	}

	public enum loadResult {
		error(2), empty(3), success(4);
		int value;

		loadResult(int value) {
			this.value = value;
		}

		public int getValue() {
			return value;
		}

		public void setValue(int value) {
			this.value = value;
		}

	}

	// 根据服务器的数据,切换状态
	public void show() {
		if (state == STATE_ERROR || state == STATE_EMPTY) {
			state = STATE_LOADING;
		}
		// 请求服务器,根据服务器返回的状态改变state

		ThreadManager.getInstance().createLongPool().execute(new Runnable() {
			public void run() {
				SystemClock.sleep(2000);
				final loadResult result = load();

				UiUtils.runOnUiThread(new Runnable() {

					@Override
					public void run() {

						if (result != null) {
							state = result.getValue();
							showPage();// 状态改变了,重新判断当前应该显示哪个界面
						}
					}
				});

			};

		});

		showPage();

	}

	/**
	 * 创建成功界面
	 * 
	 * @return
	 */
	public abstract View createSuccessView();

	/**
	 * 请求服务器
	 * 
	 * @return
	 */
	public abstract loadResult load();

}
